﻿var totalReg = {
    _regUrl: 'ajax/Register.aspx',
    _unavaliblepassword: ',123456789,12345678,11111111,00000000,1234567890,88888888,123123123,123456,111111111,aaaaaa,aaaaaa,aaaaaaaa,987654321,147258369,iloveyou,1111111111,66666666,password,11223344,12344321,000000000,99999999,0123456789,12121212,1qaz2wsx,123456123,31415926,123654789,asdfghjkl,a123456789,qq123456,12341234,1q2w3e4r,111111,123321123,789456123,qqqqqqqq,qazwsxedc,19820619,1234,110110110,55555555,qwertyuiop,1234qwer,abcd1234,12345678A,0000000000,123456123456,33333333,zzzzzzzz,87654321,zhangjianian,abc123456,10101010,asdfghjk,77777777,123456789a,999999999,kent123456,qwertyui,llllllll,123456abc,521521521,lovesunnyleefe,1111,q1w2e3r4,123698745,xiazhili,qweqweqwe,zhen7919238,asd123456,403403328,zxcvbnm123,WOAINI1314,shanghai,19881202,jingjing,584131420,123456987,321321321,19781015,weikkkkk,3.1415926,7894561230,aa123456,111222333,shens1d2gf,f19841205,15871718415,qwer1234,a12345678,151561614,buzhidao,369258147,5845201314,asdfasdf,qwe123456,qweasdzxc,',

    reg: function ()
    {
        if ($('#email_wrong').css('display') != 'none' || $('#username_wrong').css('display') != 'none')
        {
            return false;
        }
        if (!(this.checkEmail(false) && this.checkPassword() && this.checkPassword2() && this.checkUsername(false) && this.checkNickName() && this.checkCapt() && this.checkAgree()))
        {
            return false;
        }

        var username = $('#username').val();
        var nickname = $('#nickname').val();
        var password = $('#password').val();
        var email = $('#email').val();
        var captcode = $('#captcode').val();
        var frienduserid = document.getElementById("ctl00_ContentPlaceHolder1_hidFriendUserID").value;
        $.ajax({
            type: "GET",
            cache: false,
            url: this._regUrl,
            data: ({ username: username, nickname: nickname, password: password, email: email, captcode: captcode, frienduserid: frienduserid }),
            dataType: 'html',
            success: function (obj)
            {
                if (obj == '1')
                {
                    var Request = new Object();
                    Request = GetRequest();
                    if (Request != null)
                    {
                        var returnurl = Request['returnurl'];
                        if (returnurl != null)
                        {
                            if (returnurl.length > 0)
                            {
                                window.location.href = returnurl;
                            }
                            else
                            {
                                window.location.href = 'Index.aspx';
                            }
                        }
                        else
                        {
                            window.location.href = 'Index.aspx';
                        }
                    }
                    else
                    {
                        window.location.href = 'Index.aspx';
                    }
                    //                    window.location = 'Index.aspx';
                    return true;
                }
                if (obj == '2')
                {
                    totalReg.setWrong('captcode', '验证码错误');
                    chgCap();
                    return;
                }
                chgCap();
                alert('注册失败');
                return;

            }
        });
    },
    _isEmail: function (str)
    {
        var ename = str.split('@').shift(),
		fisrtCharPattern = lastCharPattern = /[a-zA-Z0-9]/,
		pattern = /^([a-zA-Z0-9-_.]){2,64}@([a-zA-Z0-9.]){2,}(\.(com|cn|net|org|info|edu|mil|gov|biz|us|tv|cc)){1}$/i;
        return (str.length <= 50 && pattern.test(str) && fisrtCharPattern.test(ename.substr(0, 1)) && lastCharPattern.test(ename.substr(ename.length - 1, 1))) ? true : false;
    },
    checkEmail: function (remoteCheck)
    {
        var email = $('#email').val();
        this.setRight('email');
        if ($.trim(email) == '')
        {
            this.setWrong('email', '邮箱不能为空');
            return false;
        }
        if (!this._isEmail(email))
        {
            this.setWrong('email', '邮箱格式错误');
            return false;
        }
        if (remoteCheck)
        {
            $.get('ajax/CheckEmail.aspx?code=' + Math.random() + '&email=' + encodeURI(email),
				function (result)
				{
				    if (result == 'repeat')
				    {
				        totalReg.setWrong('email', '邮箱已被注册');
				        return false;
				    }
				    if (result == 'normal')
				    {
				        totalReg.setRight('email');
				        return true;
				    }
				    totalReg.setWrong('email', '网络异常');
				    return false;

				}
			);
        } else
        {
            this.setRight('email');
        }
        return true;
    },
    checkPassword: function ()
    {
        var password = $('#password').val();
        var password2 = $('#password2').val();
        if (password.length < 6 || password.length > 16)
        {
            this.setWrong('password', '密码长度应为6-16位');
            this.setInit('password2');
            return false;
        }
        if ((this._unavaliblepassword).indexOf(',' + password + ',') != -1)
        {
            this.setWrong('password', '密码太简单，请换一个');
            this.setInit('password2');
            return false;
        }
        this.setRight('password');
        if (password2 != '')
        {
            return this.checkPassword2();
        }
        return true;
    },
    checkPassword2: function ()
    {
        var password = $('#password').val();
        var password2 = $('#password2').val();
        this.setInit('password2');
        if (password2 == '')
        {
            this.setWrong('password2', '请再确认一次密码');
            return false;
        }
        if (password != password2)
        {
            this.setWrong('password2', '密码不相同');
            return false;
        }
        this.setRight('password2');
        return true;
    },
    checkNickName: function ()
    {
        var nickname = $('#nickname').val();
        this.setInit('nickname');
        if ($.trim(nickname) == '')
        {
            this.setWrong('nickname', '昵称不能为空');
            return false;
        }
        if (nickname.indexOf(' ') != -1 || nickname.indexOf('　') != -1)
        {
            this.setWrong('nickname', '昵称不能包含空格');
            return false;
        }
        var l = this.charLength(nickname);
        if (l < 1 || l > 10)
        {
            this.setWrong('nickname', '用户名长度应在1-10个字符之间');
            return false;
        }
        this.setRight('nickname');
        return true;
    },
    checkUsername: function (remoteCheck)
    {
        var username = $('#username').val();
        this.setInit('username');
        if ($.trim(username) == '')
        {
            this.setWrong('username', '用户名不能为空');
            return false;
        }
        if (username.indexOf(' ') != -1 || username.indexOf('　') != -1)
        {
            this.setWrong('username', '用户名不能包含空格');
            return false;
        }
        if (!is_zimushuzi(username))
        {
            this.setWrong('username', '用户名格式不正确，应为字母或数字');
            return false;
        }
//        if (('0123456789').indexOf(username.substring(0, 1)) != -1)
//        {
//            this.setWrong('username', '用户名不能以数字开头');
//            return false;
//        }
        var l = this.charLength(username);
        if (l < 4 || l > 15)
        {
            this.setWrong('username', '用户名长度应在4-15个字符之间');
            return false;
        }
        if (remoteCheck)
        {
            $.get('ajax/CheckUserName.aspx?code=' + Math.random() + '&username=' + encodeURI(username),
				function (result)
				{
				    if (result == 'repeat')
				    {
				        totalReg.setWrong('username', '用户名已被使用');
				        return false;
				    }
				    if (result == 'normal')
				    {
				        totalReg.setRight('username');
				    }
				}
			);
        } else
        {
            this.setRight('username');
        }
        return true;
    },
    checkCapt: function ()
    {
        var captcode = $('#captcode').val();
        var r = new RegExp(/^[a-zA-Z0-9]{4}$/);
        if (r.test(captcode))
        {
            this.setRight('captcode');
            return true;
        }
        this.setWrong('captcode', '请输入验证码');
        return false;
    },
    checkAgree: function ()
    {
        this.setInit('agree');
        if (document.getElementById('agree').checked)
        {
            return true;
        } else
        {
            this.setWrong('agree', '请阅读并同意用户协议');
        }
    },
    charLength: function (str)
    {
        var realLength = 0, len = str.length, charCode = -1;
        for (var i = 0; i < len; i++)
        {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128) realLength += 1;
            else realLength += 2;
        }
        return realLength;
    },
    setWrong: function (id, msg)
    {
        $('#' + id + "_right").hide();
        $('#' + id + "_wrong").html(msg + '<em></em>').show();
        $('#' + id + "_label").hide();
    },
    setRight: function (id)
    {
        $('#' + id + "_right").show();
        $('#' + id + "_wrong").html('').hide();
        $('#' + id + "_label").hide();
    },
    setInit: function (id)
    {
        $('#' + id + "_right").hide();
        $('#' + id + "_wrong").html('').hide();
        $('#' + id + "_label").show();
    }

}


function chgCap()
{
    document.getElementById('captcha').src = "ajax/GetCodeImg.aspx?code=" + Math.random();
    document.getElementById('captcode').value = '';
}
$('#email').val('');
$('#username').val('');
chgCap();

$(document).ready(
	function ()
	{
	    $('#regbutton').click(function () { totalReg.reg(); return false; });
	}
)

function goToUrl(url)
{
    window.location = url;
}
